package com.momoauth.common.swagger.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.momoauth.common.swagger.support.Knife4jProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(Knife4jProperties.class)
@EnableOpenApi
@EnableKnife4j
public class Knife4jConfig {

    /**
     * 接口文档地址：http://域名:端口号/doc.html（请通过网关访问）
     */

    @Value("${swagger.enable}")
    private boolean enable;

    @Bean
    public Docket createRestApi(Knife4jProperties properties) {
        return new Docket(DocumentationType.OAS_30)
                .enable(enable)
                .apiInfo(apiInfo(properties))
                .select()
                .apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(Knife4jProperties properties) {
        return new ApiInfoBuilder()
                .title(properties.getTitle())
                .version(properties.getVersion())
                .description(properties.getDescription())
                .build();
    }
}
